﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bing;

namespace BingTranslatorPlugin
{
    public partial class frmMain : Form
    {
        private string _appID = "";

        public frmMain(string appID)
        {
            InitializeComponent();
            _appID = appID;
            cbFrom.DataSource = new BindingSource(GetLanguages(), null);
            cbFrom.DisplayMember = "value";
            cbFrom.ValueMember = "key";
            cbFrom.SelectedIndex = 4;
            cbTo.DataSource = new BindingSource(GetLanguages(), null);
            cbTo.DisplayMember = "value";
            cbTo.ValueMember = "key";
            cbTo.SelectedIndex = 6;
        }

        public string OriginalText { get; set; }

        public string TranslatedText { get; set; }

        public bool UseQuotes { get; set; }

        public bool UseOriginalAsTitle { get; set; }

        private void pbLogo_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.bing.com/");
        }

        public static Dictionary<string, string> GetLanguages()
        {
            Dictionary<string, string> languages = new Dictionary<string, string>();
            languages.Add("Ar", "Arabic");
            languages.Add("zh-CHS", "Simplified Chinese");
            languages.Add("zh-CHT", "Traditional Chinese");
            languages.Add("Nl", "Dutch");
            languages.Add("En", "English");
            languages.Add("Fr", "French");
            languages.Add("De", "German");
            languages.Add("It", "Italian");
            languages.Add("Ja", "Japanese");
            languages.Add("Ko", "Korean");
            languages.Add("Pl", "Polish");
            languages.Add("Pt", "Portuguese");
            languages.Add("Ru", "Russian");
            languages.Add("Es", "Spanish");
            return languages;
        }

        private void pbSwap_Click(object sender, EventArgs e)
        {
            int from = cbFrom.SelectedIndex;
            int to = cbTo.SelectedIndex;
            cbFrom.SelectedIndex = to;
            cbTo.SelectedIndex = from;
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            string origText = txbxOrigText.Text;
            if (origText.Length == 0)
            {
                MessageBox.Show("Sorry, no text to translate", "Big Fat Hairy Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SearchRequest searchRequest = new SearchRequest() { AppId = _appID, Query = origText.Trim(), Market = "en-US" };

            TranslationRequest translationRequest = new TranslationRequest();
            translationRequest.SourceLanguage = cbFrom.SelectedValue.ToString();
            translationRequest.TargetLanguage = cbTo.SelectedValue.ToString();

            TranslationResponse response = API.Translation(searchRequest, translationRequest);

            if (response.Errors.Count > 0)
            {
                // Display Errors
                MessageBox.Show(bingErrorMessage(response.Errors), "Bing.API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (response.TranslationResults.Count > 0)
            {
                txbxTranslatedText.Text = response.TranslationResults[0].TranslatedTerm;
            }

        }

        private static string bingErrorMessage(IEnumerable<Error> errors)
        {
            StringBuilder text = new StringBuilder();
            int counter = 1;
            foreach (Error error in errors)
            {
                text.AppendFormat("Error #{0}.\r\n", counter);
                text.AppendFormat("\tMessage: {0}\r\n", error.Message);
                text.AppendFormat("\tCode: {0}\r\n", error.Code);
                text.AppendFormat("\tHelpUrl: {0}\r\n", error.HelpUrl);
                text.AppendFormat("\tParameter: {0}\r\n", error.Parameter);
                text.AppendFormat("\tValue: {0}\r\n", error.Value);
                counter++;
            }

            return text.ToString();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (txbxOrigText.Text.Length == 0 || txbxTranslatedText.Text.Length == 0)
            {
                MessageBox.Show("There is nothing to insert.", "Big Fat Hairy Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            TranslatedText = txbxTranslatedText.Text;
            OriginalText = txbxOrigText.Text;
            UseQuotes = chbxUseQuotes.Checked;
            UseOriginalAsTitle = chbxOrigTitle.Checked;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }


    }
}
